﻿Public Class Panel
    Inherits System.Windows.Forms.Panel

    Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC" (ByVal hwnd As IntPtr) _
As IntPtr
    Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As IntPtr, _
ByVal hdc As IntPtr) As Integer
    Private m_BorderColor As Color = Color.Black
    Private m_Text As String


    Public Sub New()
        'MyBase.New()
        'Me.SetStyle(ControlStyles.UserPaint, False)
        'Me.SetStyle(ControlStyles.DoubleBuffer, True)
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Const WM_NCPAINT = &H85

        If m.Msg = WM_NCPAINT Then

            Dim hdc As IntPtr = GetWindowDC(m.HWnd)
            Dim g As Graphics = Graphics.FromHdc(hdc)
            Dim rDraw As Rectangle = New Rectangle(0, 0, Me.Width - 1, Me.Height - 1)

            If Me.BorderStyle = Windows.Forms.BorderStyle.Fixed3D Then


                Dim pBottom As Pen = New Pen(Color.Gray, 2)
                Dim pTop As Pen = New Pen(Color.White, 2)

                g.DrawRectangle(pBottom, rDraw)

                Dim pts(2) As Point

                pts(0) = New Point(0, Me.Height - 1)
                pts(1) = New Point(0, 0)
                pts(2) = New Point(Me.Width - 1, 0)

                g.DrawLines(pTop, pts)
                ReleaseDC(Me.Handle, hdc)

            Else
                ReleaseDC(Me.Handle, hdc)
            End If
        Else

            MyBase.WndProc(m)
        End If


    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

        If Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle Then
            ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, m_BorderColor, ButtonBorderStyle.Solid)
            MyBase.OnPaint(e)
        End If

        DrawText(e)

    End Sub


    Public Property BorderColor() As Color
        Get
            Return m_BorderColor
        End Get
        Set(ByVal Value As Color)
            m_BorderColor = Value
        End Set
    End Property

    Public Property _Caption() As String
        Get
            Return m_Text
        End Get
        Set(ByVal Value As String)
            m_Text = Value
        End Set
    End Property


    Private Sub DrawText(ByVal e As PaintEventArgs)
        Using string_format As New StringFormat()
            string_format.Alignment = StringAlignment.Center
            string_format.LineAlignment = StringAlignment.Center



            'System.Drawing.FontFamily = Me.Font.FontFamily

            e.Graphics.DrawString(m_Text, New Font(Me.Font.FontFamily, Me.Font.Size, FontStyle.Regular, GraphicsUnit.Point, Nothing), New System.Drawing.SolidBrush(Me.ForeColor), Convert.ToInt32(Me.ClientSize.Width / 2), Convert.ToInt32(Me.ClientSize.Height / 2), string_format)
        End Using
    End Sub

End Class
